package com.tedu.project5.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tedu.project5.common.Result;
import com.tedu.project5.dto.PurchaseInfoDTO;
import com.tedu.project5.dto.QueryPurchaseInfoDTO;
import com.tedu.project5.model.DdPurchaseInfo;
import com.tedu.project5.mapper.DdPurchaseInfoMapper;
import com.tedu.project5.service.IDdPurchaseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 采购表 服务实现类
 * </p>
 *
 * @author mhg
 * @since 2022-06-20
 */
@Slf4j
@Service
public class DdPurchaseInfoServiceImpl extends ServiceImpl<DdPurchaseInfoMapper, DdPurchaseInfo> implements IDdPurchaseInfoService {
    
    @Autowired(required = false)
    private DdPurchaseInfoMapper ddPurchaseInfoMapper;
    
    /**
     * 查询采购列表
     * @return
     */
    @Override
    public Result queryExample(QueryPurchaseInfoDTO dto) {
        QueryWrapper<DdPurchaseInfo> wrapper = new QueryWrapper<>();
        //查询条件
        if (dto.getPurchaseName()!=null){
            //第一个参数是表里的字段名，第二个参数是前端传过来的值
            wrapper.like("purchase_name",dto.getPurchaseName());
        }
        if (dto.getPurchaseType()!=null){
            //第一个参数是表里的字段名，第二个参数是前端传过来的值
            wrapper.like("purchase_type",dto.getPurchaseType());
        }
        if (dto.getTotalPrice()!=null){
            //第一个参数是表里的字段名，第二个参数是前端传过来的值
            wrapper.eq("total_price",dto.getTotalPrice());
        }
        if (dto.getTotalNum()!=null){
            //第一个参数是表里的字段名，第二个参数是前端传过来的值
            wrapper.eq("total_num",dto.getTotalNum());
        }
        List<DdPurchaseInfo> ddPurchaseInfos = ddPurchaseInfoMapper.selectList(wrapper);
        return Result.ok(ddPurchaseInfos);
    }

    /**
     * 新增采购
     * @param purchaseInfo
     * @return
     */
    @Override
    public Result savePurchase(DdPurchaseInfo purchaseInfo) {
        if (purchaseInfo.getPurchaseName()!=null && purchaseInfo.getPurchaseType()!=null && purchaseInfo.getTotalPrice()!=null && purchaseInfo.getTotalNum()!=null){
            int insert = ddPurchaseInfoMapper.insert(purchaseInfo);
            log.info("插入结果为--："+insert);
            if (insert>=0){
                return Result.ok();
            }else{
                return Result.error("插入失败");
            }
        }else{
            return Result.error("参数缺失");
        }
    }

    /**
     * 更新采购信息
     * @param dto
     * @return
     */
    @Override
    public Result updatePurchase(PurchaseInfoDTO dto) {
        //校验数据合法性
        if (dto!=null && dto.getPurchaseName()!=null && dto.getPurchaseType()!=null && dto.getTotalPrice()!=null && dto.getTotalNum()!=null){
            //组装数据
            DdPurchaseInfo pojo = new DdPurchaseInfo();
            pojo.setId(dto.getId());
            pojo.setPurchaseName(dto.getPurchaseName());
            pojo.setPurchaseType(dto.getPurchaseType());
            pojo.setTotalPrice(dto.getTotalPrice());
            pojo.setTotalNum(dto.getTotalNum());
            //保存到数据库
            int i = ddPurchaseInfoMapper.updateById(pojo);
            if (i!=0){
                return Result.ok();
            }else{
                return Result.error("更新失败");
            }

        }else{
            return Result.error("数据不合法！");
        }
    }

    /**
     * 删除采购信息
     * @param id
     * @return
     */
    @Override
    public Result deletePurchase(String id) {
        int i = ddPurchaseInfoMapper.deleteById(id);
        if (i>=0){
            return Result.ok();
        }else{
            return Result.error("删除失败");
        }
    }
}
